package com.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.portal.entity.Case;
import com.portal.mapper.CaseMapper;
import com.portal.service.CaseService;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 案例服务实现类
 */
@Service
public class CaseServiceImpl extends ServiceImpl<CaseMapper, Case> implements CaseService {
    
    @Override
    public Map<String, Object> getCaseList(Long categoryId, Integer page, Integer size) {
        // 构建查询条件
        LambdaQueryWrapper<Case> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Case::getEnabled, true);
        if (categoryId != null) {
            wrapper.eq(Case::getCategoryId, categoryId);
        }
        wrapper.orderByDesc(Case::getCreateTime);

        // 分页查询
        Page<Case> casePage = new Page<>(page, size);
        page(casePage, wrapper);

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("total", casePage.getTotal());
        result.put("list", casePage.getRecords());

        return result;
    }
    
    @Override
    public Case getCaseDetail(Long id) {
        return getById(id);
    }
    
    @Override
    public List<Case> getRecommendCases(int limit) {
        LambdaQueryWrapper<Case> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Case::getEnabled, true)
               .eq(Case::getRecommended, true)
               .orderByDesc(Case::getCreateTime)
               .last("LIMIT " + limit);
        return list(wrapper);
    }

    @Override
    public List<Case> getCustomerCases() {
        LambdaQueryWrapper<Case> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Case::getEnabled, true)
               .eq(Case::getType, "CUSTOMER")
               .orderByDesc(Case::getCreateTime);
        return list(wrapper);
    }

    @Override
    public List<String> getCaseCategories() {
        LambdaQueryWrapper<Case> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Case::getEnabled, true)
               .select(Case::getCategory)
               .groupBy(Case::getCategory);
        return listObjs(wrapper, Object::toString);
    }
} 